@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Forms
@using Radzen
@using Radzen.Blazor.Rendering
@using System.Collections
@typeparam TItem
@inherits PagedDataBoundComponent<TItem>

@functions {
    private bool IsRowHeaderExpanded(int level, List<string> parentValues)
    {
        if (level == 0) return true;
        var pathKey = string.Join("|", parentValues);
        return !_collapsedRowGroups.ContainsKey(pathKey) || !_collapsedRowGroups[pathKey];
    }
}

<CascadingValue Value=this>
@if (Columns != null)
{
    @Columns
}
@if (Rows != null)
{
    @Rows
}
@if (Aggregates != null)
{
    @Aggregates
}
</CascadingValue>

@if (Visible)
{
    <div @ref=@Element style="@Style" @attributes="Attributes" class="rz-pivot-data-grid @GetCssClass()" id="@GetId()" tabindex=0>
        @if(AllowFieldsPicking)
        {
            <RadzenPanel AllowCollapse="true" Collapsed=@(!FieldsPickerExpanded)>
                <HeaderTemplate>
                    @if(FieldsPickerHeaderTemplate != null)
                    {
                        @FieldsPickerHeaderTemplate                        
                    }
                    else
                    {
                        @FieldsPickerHeaderText
                    }
                </HeaderTemplate>
                <ChildContent>
                    <RadzenRow>
                        <RadzenColumn SizeMD="3">
                            <RadzenLabel Component="Rows" Text="@RowsText" />
                            <RadzenDropDown Name="Rows" Data="@pivotFields" TextProperty="Title" ValueProperty="Property" @bind-Value="@selectedRows"
                                Multiple="true" Style="width: 100%;" Change="@(args => UpdateFieldsFromSelected())" 
                                AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" />
                        </RadzenColumn>
                        <RadzenColumn SizeMD="3">
                            <RadzenLabel Component="Columns" Text="@ColumnsText" />
                            <RadzenDropDown Name="Columns" Data="@pivotFields" TextProperty="Title" ValueProperty="Property" @bind-Value="@selectedColumns"
                                            Multiple="true" Style="width: 100%;" Change="@(args => UpdateFieldsFromSelected())"
                                            AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" />
                        </RadzenColumn>
                        <RadzenColumn SizeMD="6">
                            <RadzenLabel Component="Aggregates" Text="@AggregatesText" />
                            <RadzenDropDown Name="Aggregates" Data="@pivotFields" TextProperty="Title" ValueProperty="Property" MaxSelectedLabels="2" Multiple="true" Change=@UpdateAggregates
                                TValue="IEnumerable<string>" Style="width: 100%;" Value="@(selectedAggregates.Select(a => a.Property).Distinct())"
                                    AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" >
                                <Template>
                                    <RadzenLabel Component="Aggregate" Text="@context.Title" />
                                    <RadzenSelectBar Name="Aggregate" TValue="AggregateFunction?" Data="@GetAggregates(context.Property)" 
                                        Size="ButtonSize.ExtraSmall" @onclick:stopPropagation="true" Style="width: 100%;"
                                        Value="@(selectedAggregates.FirstOrDefault(a => a.Property == context.Property)?.Aggregate)"
                                        Change="@(args => UpdateAggregate((string)context.Property, args))" />
                                </Template>
                                <ValueTemplate>
                                    @string.Join(",", selectedAggregates.Select(a => $"{a.Property} ({Enum.GetName(a.Aggregate)})"))
                                </ValueTemplate>
                            </RadzenDropDown>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenRow>
                        <RadzenColumn SizeMD="3">
                          <table class="rz-pivot-table @GetTableCssClass()">
                           <thead class="rz-pivot-header">
                            @foreach(var selectedRow in selectedRows)
                            {
                                var row = pivotRows.FirstOrDefault(r => r.Property == selectedRow);
                                var isSortable = AllowSorting && row?.Sortable == true;
                                var isFilterable = AllowFiltering && row?.Filterable == true;
                                var sortOrder = row?.GetSortOrder();
                                var hasActiveFilter = row?.HasActiveFilter() == true;
                                <tr class="rz-pivot-header-row">
                                <th class="rz-pivot-row-header @(isSortable ? "rz-sortable-column" : "")" style="width:100%;margin-top:5px;">
                                    <div class="rz-pivot-header-content-wrapper">
                                        <div @onclick="@(args => OnRowSort(args, row))"
                                                @onkeydown="@(isSortable ? OnSortKeyPressed : (Func<KeyboardEventArgs, Task>)null)"
                                                class="rz-pivot-header-content @(isSortable ? "rz-sortable" : "")"
                                                tabindex="@(isSortable ? "0" : null)"
                                                role="@(isSortable ? "button" : null)"
                                                aria-label="@(isSortable ? $"Sort by {row?.GetTitle()}" : null)">
                                            <span class="rz-pivot-header-text">@row?.GetTitle()</span>
                                            @if (isSortable)
                                            {
                                                @if (sortOrder == SortOrder.Ascending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-asc"></span>
                                                }
                                                else if (sortOrder == SortOrder.Descending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-desc"></span>
                                                }
                                                else
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort"></span>
                                                }
                                            }
                                        </div>
                                        @if (isFilterable)
                                        {
                                            <i @ref="FilterIconRef[row]" @onclick:stopPropagation="true" 
                                                @onmousedown="@(() => ToggleFilter(row))"
                                                class="@GetFilterIconCss(row)"
                                                title="Filter">
                                                @FilterIcon
                                            </i>
                                        }
                                    </div>
                                </th>
                                </tr>
                            }
                            </thead>
                           </table>
                        </RadzenColumn>
                        <RadzenColumn SizeMD="3">
                          <table class="rz-pivot-table @GetTableCssClass()">
                           <thead class="rz-pivot-header">
                            @foreach (var selectedColumn in selectedColumns)
                            {
                                var column = pivotColumns.FirstOrDefault(c => c.Property == selectedColumn);
                                var isSortable = AllowSorting && column?.Sortable == true;
                                var isFilterable = AllowFiltering && column?.Filterable == true;
                                var sortOrder = column?.GetSortOrder();
                                var hasActiveFilter = column?.HasActiveFilter() == true;

                                <tr class="rz-pivot-header-row">
                                <th class="rz-pivot-column-header @(isSortable ? "rz-sortable-column" : "")" style="width:100%;margin-top:5px;">
                                    <div class="rz-pivot-header-content-wrapper">
                                        <div @onclick="@(args => OnColumnSort(args, column))"
                                                @onkeydown="@(isSortable ? OnSortKeyPressed : (Func<KeyboardEventArgs, Task>)null)"
                                                class="rz-pivot-header-content @(isSortable ? "rz-sortable" : "")"
                                                tabindex="@(isSortable ? "0" : null)"
                                                role="@(isSortable ? "button" : null)"
                                                aria-label="@(isSortable ? $"Sort by {column?.GetTitle()}" : null)">
                                            <span class="rz-pivot-header-text">@column?.GetTitle()</span>
                                            @if (isSortable)
                                            {
                                                @if (sortOrder == SortOrder.Ascending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-asc"></span>
                                                }
                                                else if (sortOrder == SortOrder.Descending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-desc"></span>
                                                }
                                                else
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort"></span>
                                                }
                                            }
                                        </div>
                                        @if (isFilterable)
                                        {
                                            <i @ref="FilterIconRef[column]" @onclick:stopPropagation="true" 
                                                @onmousedown="@(() => ToggleFilter(column))"
                                                class="@GetFilterIconCss(column)"
                                                title="Filter">
                                                @FilterIcon
                                            </i>
                                        }
                                    </div>
                                </th>
                                </tr>
                            }
                           </thead>
                          </table>
                        </RadzenColumn>
                        <RadzenColumn SizeMD="6">
                           <table class="rz-pivot-table @GetTableCssClass()">
                            <thead class="rz-pivot-header">
                            @foreach (var agg in selectedAggregates)
                            {
                                var column = pivotAggregates.FirstOrDefault(c => c.Property == agg.Property);
                                var isSortable = AllowSorting && column?.Sortable == true;
                                var isFilterable = AllowFiltering && column?.Filterable == true;
                                var sortOrder = column?.GetSortOrder();
                                var hasActiveFilter = column?.HasActiveFilter() == true;

                                <tr class="rz-pivot-header-row">
                                <th class="rz-pivot-column-header @(isSortable ? "rz-sortable-column" : "")" style="width:100%;margin-top:5px;">
                                    <div class="rz-pivot-header-content-wrapper">
                                        <div @onclick="@(args => OnColumnSort(args, column))"
                                                @onkeydown="@(isSortable ? OnSortKeyPressed : (Func<KeyboardEventArgs, Task>)null)"
                                                class="rz-pivot-header-content @(isSortable ? "rz-sortable" : "")"
                                                tabindex="@(isSortable ? "0" : null)"
                                                role="@(isSortable ? "button" : null)"
                                                aria-label="@(isSortable ? $"Sort by {column?.GetTitle()}" : null)">
                                            <span class="rz-pivot-header-text">@column?.GetTitle()</span>
                                            @if (isSortable)
                                            {
                                                @if (sortOrder == SortOrder.Ascending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-asc"></span>
                                                }
                                                else if (sortOrder == SortOrder.Descending)
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort rzi-sort-desc"></span>
                                                }
                                                else
                                                {
                                                    <span class="notranslate rz-sortable-column-icon rzi-grid-sort rzi-sort"></span>
                                                }
                                            }
                                        </div>
                                        @if (isFilterable)
                                        {
                                            <i @ref="FilterIconRef[column]" @onclick:stopPropagation="true" 
                                                @onmousedown="@(() => ToggleFilter(column))"
                                                class="@GetFilterIconCss(column)"
                                                title="Filter">
                                                @FilterIcon
                                            </i>
                                        }
                                    </div>
                                </th>
                                </tr>
                            }
                            </thead>
                          </table>
                        </RadzenColumn>
                    </RadzenRow>
                </ChildContent>
            </RadzenPanel>
        }
        @if (AllowPaging && PagerPosition.HasFlag(PagerPosition.Top))
        {
            <RadzenPager HorizontalAlign="@PagerHorizontalAlign" AlwaysVisible="@PagerAlwaysVisible" @ref="topPager"
                         Count="@Count" PageSize="@PageSize" PageNumbersCount="@PageNumbersCount"
                         PageChanged="@OnPageChanged" PageSizeChanged="@OnPageSizeChanged"
                         PageSizeOptions="@PageSizeOptions" ShowPagingSummary="@ShowPagingSummary"
                         PagingSummaryFormat="@PagingSummaryFormat" PagingSummaryTemplate="@PagingSummaryTemplate"
                         PageSizeText="@PageSizeText" Density="@Density"
                         FirstPageTitle="@FirstPageTitle" FirstPageAriaLabel="@FirstPageAriaLabel"
                         PrevPageAriaLabel="@PrevPageAriaLabel" PrevPageTitle="@PrevPageTitle"
                         NextPageAriaLabel="@NextPageAriaLabel" NextPageTitle="@NextPageTitle"
                         LastPageAriaLabel="@LastPageAriaLabel" LastPageTitle="@LastPageTitle"
                         PageAriaLabelFormat="@PageAriaLabelFormat" PageTitleFormat="@PageTitleFormat"
                         PrevPageLabel="@PrevPageLabel" NextPageLabel="@NextPageLabel" />
        }

        <div class="rz-pivot-content">
            @if (View.Count() > 0)
            {
                <div class="rz-pivot-table-container">
                    <table class="rz-pivot-table @GetTableCssClass()">
                        <thead class="rz-pivot-header">
                            @foreach (var headerRow in GetColumnHeaderRows())
                            {
                                <tr class="rz-pivot-header-row">
                                    @if (headerRow == 0)
                                    {
                                        var rowHeaderCellsCount = CachedPivotRows.FirstOrDefault() != null ?
                                            Math.Min(CachedPivotRows.Select(r => r.RowHeaderCells.Count).Max(), pivotRows.Count) : pivotRows.Count;
                                        @for (int i = 0; i < rowHeaderCellsCount; i++)
                                        {
                                            var row = pivotRows[i];
                                            <th class="rz-pivot-row-header @GetFrozenRowHeaderClass(i)"
                                                rowspan="@(pivotColumns.Count + 1)"
                                                style="inset-inline-start: @(i * 140)px">
                                                <div class="rz-pivot-header-content-wrapper">
                                                    <div class="rz-pivot-header-content">
                                                        <span class="rz-pivot-header-text">@row.GetTitle()</span>
                                                    </div>
                                                </div>
                                            </th>
                                        }
                                    }
                                    @foreach (var cell in GetColumnHeaderCells(headerRow))
                                    {
                                        <th class="rz-pivot-column-header" 
                                            colspan="@(cell.ColSpan * pivotAggregates.Count)" 
                                            rowspan="@cell.RowSpan"
                                            style="@(!string.IsNullOrEmpty(cell.Width) ? $"width: {cell.Width};" : "")">
                                            @if (AllowDrillDown && !string.IsNullOrEmpty(cell.PathKey) && cell.HasChildren)
                                            {
                                                <div class="rz-pivot-drill-down-header">
                                                    <span @onclick="@(() => ToggleColumnDrillDown(cell.PathKey))"
                                                        class="notranslate rz-tree-toggler rzi rzi-caret-@(cell.IsCollapsed ? "right" : "down")" style="margin-inline-start:0"></span>
                                                    <span class="rz-pivot-header-text">
                                                    @if (cell.HeaderTemplate != null)
                                                    {
                                                        @cell.HeaderTemplate(cell.Group)
                                                    }
                                                    else
                                                    {
                                                        @cell.Title
                                                    }
                                                    </span>
                                                </div>
                                            }
                                            else
                                            {
                                                <div class="rz-pivot-header-content-wrapper">
                                                    <div class="rz-pivot-header-content">
                                                        <span class="rz-pivot-header-text">
                                                        @if (cell.HeaderTemplate != null)
                                                        {
                                                            @cell.HeaderTemplate(cell.Group)
                                                        }
                                                        else
                                                        {
                                                            @cell.Title
                                                        }
                                                        </span>
                                                    </div>
                                                </div>
                                            }
                                        </th>
                                    }
                                    @if (headerRow == 0 && ShowRowsTotals)
                                    {
                                        foreach (var aggregate in pivotAggregates)
                                        {
                                            <th class="rz-pivot-total-header @GetFrozenTotalHeaderClass()" 
                                                rowspan="@(pivotColumns.Count + 1)"
                                                style="inset-inline-end: @((pivotAggregates.Count - 1 - pivotAggregates.IndexOf(aggregate)) * 120)px">
                                                @if (aggregate.HeaderTemplate != null)
                                                {
                                                    @aggregate.HeaderTemplate
                                                }
                                                else
                                                {
                                                    @aggregate.GetTitle()
                                                }
                                            </th>
                                        }
                                    }
                                </tr>
                            }
                            <tr class="rz-pivot-header-row rz-pivot-aggregate-header-row">
                                @foreach (var colPath in CachedColumnLeaves)
                                {
                                    foreach (var aggregate in pivotAggregates)
                                    {
                                        <th class="rz-pivot-aggregate-header">
                                            @if (aggregate.HeaderTemplate != null)
                                            {
                                                @aggregate.HeaderTemplate
                                            }
                                            else
                                            {
                                                @aggregate.GetTitle()
                                            }
                                        </th>
                                    }
                                }
                            </tr>
                        </thead>
                        <tbody class="rz-pivot-body">
                            @RenderPivotRows()
                        </tbody>
                        @if (ShowColumnsTotals)
                        {
                            <tfoot class="rz-pivot-footer">
                                @RenderFooter()
                            </tfoot>
                        }
                    </table>
                </div>
            }
            else
            {
                @if (EmptyTemplate != null)
                {
                    @EmptyTemplate
                }
                else
                {
                    <div class="rz-pivot-empty">
                        <RadzenText TextStyle="TextStyle.Body1" class="rz-text-color-secondary">
                            @EmptyText
                        </RadzenText>
                    </div>
                }
            }
        @if (IsLoading)
        {
            <div class="rz-datatable-loading" style="z-index:5"></div>
            <div class="rz-datatable-loading-content" style="z-index:5">
                @if(LoadingTemplate != null)
                {
                    @LoadingTemplate
                }
                else
                {
                    <i class="notranslate rzi-circle-o-notch"></i>
                }
            </div>
        }
        </div>
        @if (AllowPaging && PagerPosition.HasFlag(PagerPosition.Bottom))
        {
            <RadzenPager HorizontalAlign="@PagerHorizontalAlign" AlwaysVisible="@PagerAlwaysVisible" @ref="bottomPager" 
                         Count="@Count" PageSize="@PageSize" PageNumbersCount="@PageNumbersCount" 
                         PageChanged="@OnPageChanged" PageSizeChanged="@OnPageSizeChanged" 
                         PageSizeOptions="@PageSizeOptions" ShowPagingSummary="@ShowPagingSummary" 
                         PagingSummaryFormat="@PagingSummaryFormat" PagingSummaryTemplate="@PagingSummaryTemplate" 
                         PageSizeText="@PageSizeText" Density="@Density" 
                         FirstPageTitle="@FirstPageTitle" FirstPageAriaLabel="@FirstPageAriaLabel" 
                         PrevPageAriaLabel="@PrevPageAriaLabel" PrevPageTitle="@PrevPageTitle" 
                         NextPageAriaLabel="@NextPageAriaLabel" NextPageTitle="@NextPageTitle" 
                         LastPageAriaLabel="@LastPageAriaLabel" LastPageTitle="@LastPageTitle" 
                         PageAriaLabelFormat="@PageAriaLabelFormat" PageTitleFormat="@PageTitleFormat" 
                         PrevPageLabel="@PrevPageLabel" NextPageLabel="@NextPageLabel" />
        }
    </div>

    <!-- Filter Popup -->
    @if (AllowFiltering && currentFilterField != null)
    {
        <Popup @ref="filterPopup" id="@($"pivot-filter-{currentFilterField.Property}")" class="rz-overlaypanel"
               style="display:none;min-width:250px;" @onkeydown="OnFilterPopupKeyPressed">
            <div class="rz-overlaypanel-content">
                @if (currentFilterField.FilterTemplate != null)
                {
                    @currentFilterField.FilterTemplate(currentFilterField)
                }
                else
                {
                    <form id="@($"pivot-filter-{currentFilterField.Property}-form")" @onsubmit="@(args => ApplyFilter())" class="rz-grid-filter">
                        <span class="rz-grid-filter-label">@FilterText</span>
                        <RadzenDropDown InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", FilterOperatorAriaLabel + GetFilterOperatorText(currentFilterField.GetSecondFilterOperator()) }})"
                                        @onclick:preventDefault="true" Data="@(GetFilterOperators(currentFilterField).Select(t => new { Value = GetFilterOperatorText(t), Key = t }))" TextProperty="Value" ValueProperty="Key" TValue="FilterOperator?" Value="@currentFilterField.GetFilterOperator()" Change="@(args => SetFilterOperator(currentFilterField, (FilterOperator)args))" />
                        @if (currentFilterField.FilterValueTemplate != null)
                        {
                            @currentFilterField.FilterValueTemplate(currentFilterField)
                        }
                        else if (PropertyAccess.IsNullableEnum(GetFilterPropertyType(currentFilterField)) || PropertyAccess.IsEnum(GetFilterPropertyType(currentFilterField)))
                        {
                            <RadzenDropDown Disabled="@(!CanSetFilterValue(currentFilterField))" AllowClear="false" AllowFiltering="false" TValue="@object"
                                            Value="@currentFilterField.GetFilterValue()" Multiple="false" Placeholder="@EnumFilterSelectText" TextProperty="Text" ValueProperty="Value" Data="@EnumExtensions.EnumAsKeyValuePair(Nullable.GetUnderlyingType(GetFilterPropertyType(currentFilterField)) ?? GetFilterPropertyType(currentFilterField), EnumFilterTranslationFunc)"
                                            Change="@(args => SetFilterValue(currentFilterField, args))"
                                            InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + FilterValueAriaLabel + currentFilterField.GetFilterValue() }})" />
                        }
                        else if (PropertyAccess.IsNumeric(GetFilterPropertyType(currentFilterField)))
                        {
                            @DrawNumericFilter(currentFilterField, false)
                        }
                        else if (PropertyAccess.IsDate(GetFilterPropertyType(currentFilterField)))
                        {
                            <RadzenDatePicker Disabled="@(!CanSetFilterValue(currentFilterField))" TValue="@object" ShowTime="@ShowTimeForDateTimeFilter(currentFilterField)" ShowTimeOkButton="true" DateFormat="@GetFilterDateFormat(currentFilterField)"
                                                Value="@currentFilterField.GetFilterValue()" Change="@(args => SetFilterValue(currentFilterField, PropertyAccess.IsDateOnly(GetFilterPropertyType(currentFilterField)) ? PropertyAccess.DateOnlyFromDateTime(args.Value) : args.Value))" AllowInput="@AllowFilterDateInput"
                                                InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + FilterValueAriaLabel + currentFilterField.GetFilterValue() }})" />
                        }
                        else if (GetFilterPropertyType(currentFilterField) == typeof(bool) || GetFilterPropertyType(currentFilterField) == typeof(bool?))
                        {
                            <RadzenCheckBox Disabled="@(!CanSetFilterValue(currentFilterField))" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + FilterValueAriaLabel + currentFilterField.GetFilterValue() }})" TriState="true" TValue="@object" Value="@currentFilterField.GetFilterValue()" Change="@(args => { SetFilterValue(currentFilterField, null); SetFilterValue(currentFilterField, args);  })" />
                        }
                        else
                        {
                            <RadzenTextBox Disabled="@(!CanSetFilterValue(currentFilterField))" id="@($"pivot-filter-{currentFilterField.Property}-sf")" aria-label="@(currentFilterField.GetTitle() + FilterValueAriaLabel + currentFilterField.GetFilterValue())" Value="@($"{currentFilterField.GetFilterValue()}")" Change="@(args => SetFilterValue(currentFilterField, args))" />
                        }

                        <RadzenDropDown @onclick:preventDefault="true" TextProperty="Text" ValueProperty="Value" Style="width: 90px" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + LogicalOperatorAriaLabel + (currentFilterField.GetLogicalFilterOperator() == LogicalFilterOperator.And ? AndOperatorText : OrOperatorText) }})"
                                        Data="@(Enum.GetValues(typeof(LogicalFilterOperator)).Cast<LogicalFilterOperator>().Select(t => new { Text = t == LogicalFilterOperator.And ? AndOperatorText : OrOperatorText, Value = t }))" TValue="LogicalFilterOperator?" Value="@currentFilterField.GetLogicalFilterOperator()" Change="@(args => SetLogicalFilterOperator(currentFilterField, (LogicalFilterOperator)args))" />

                        <RadzenDropDown InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", SecondFilterOperatorAriaLabel + GetFilterOperatorText(currentFilterField.GetSecondFilterOperator()) }})" @onclick:preventDefault="true" Data="@(GetFilterOperators(currentFilterField).Select(t => new { Value = GetFilterOperatorText(t), Key = t }))" TextProperty="Value" ValueProperty="Key" TValue="FilterOperator?" Value="@currentFilterField.GetSecondFilterOperator()" Change="@(args => SetSecondFilterOperator(currentFilterField, (FilterOperator)args))" />
                        @if (currentFilterField.SecondFilterValueTemplate != null)
                        {
                            @currentFilterField.SecondFilterValueTemplate(currentFilterField)
                        }
                        else if (PropertyAccess.IsNullableEnum(GetFilterPropertyType(currentFilterField)) || PropertyAccess.IsEnum(GetFilterPropertyType(currentFilterField)))
                        {
                            <RadzenDropDown Disabled="@(!CanSetFilterValue(currentFilterField, false))" AllowClear="false" AllowFiltering="false" TValue="@object" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + SecondFilterValueAriaLabel }})"
                                            Value="@currentFilterField.GetSecondFilterValue()" Multiple="false" Placeholder="@EnumFilterSelectText" TextProperty="Text" ValueProperty="Value" Data="@EnumExtensions.EnumAsKeyValuePair(Nullable.GetUnderlyingType(GetFilterPropertyType(currentFilterField)) ?? GetFilterPropertyType(currentFilterField), EnumFilterTranslationFunc)"
                                            Change="@(args => SetFilterValue(currentFilterField, args, false))" />
                        }
                        else if (PropertyAccess.IsNumeric(GetFilterPropertyType(currentFilterField)))
                        {
                            @DrawNumericFilter(currentFilterField, false, false)
                        }
                        else if (PropertyAccess.IsDate(GetFilterPropertyType(currentFilterField)))
                        {
                            <RadzenDatePicker Disabled="@(!CanSetFilterValue(currentFilterField, false))" TValue="@object" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + SecondFilterValueAriaLabel + currentFilterField.GetSecondFilterValue() }})"
                                                ShowTime="@ShowTimeForDateTimeFilter(currentFilterField)" ShowTimeOkButton="true" DateFormat="@GetFilterDateFormat(currentFilterField)"
                                                Value="@currentFilterField.GetSecondFilterValue()" Change="@(args => SetFilterValue(currentFilterField, PropertyAccess.IsDateOnly(GetFilterPropertyType(currentFilterField)) ? PropertyAccess.DateOnlyFromDateTime(args.Value) : args.Value, false))" AllowInput="@AllowFilterDateInput" />
                        }
                        else if (GetFilterPropertyType(currentFilterField) == typeof(bool) || GetFilterPropertyType(currentFilterField) == typeof(bool?))
                        {
                            <RadzenCheckBox Disabled="@(!CanSetFilterValue(currentFilterField, false))" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", currentFilterField.GetTitle() + SecondFilterValueAriaLabel + currentFilterField.GetSecondFilterValue() }})" TriState="true" TValue="@object" Value="@currentFilterField.GetSecondFilterValue()" Change="@(args => { SetFilterValue(currentFilterField, args, false);  })" />
                        }
                        else
                        {
                            <RadzenTextBox Disabled="@(!CanSetFilterValue(currentFilterField, false))" id="@($"pivot-filter-{currentFilterField.Property}-sf2")" aria-label="@(currentFilterField.GetTitle() + SecondFilterValueAriaLabel + currentFilterField.GetSecondFilterValue())" Value="@($"{currentFilterField.GetSecondFilterValue()}")" Change="@(args => SetFilterValue(currentFilterField, args, false))" />
                        }
                    </form>
                    <div class="rz-grid-filter-buttons">
                        <RadzenButton ButtonStyle="ButtonStyle.Primary" ButtonType="ButtonType.Submit" Text="@ApplyText" class="rz-button-sm" />
                        <RadzenButton ButtonStyle="ButtonStyle.Light" Text="@ClearText" Click="@ClearFilter" class="rz-button-sm" />
                    </div>
                }
            </div>
        </Popup>
    }
}